<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="joggleRule" :model="joggleForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="joggleForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="joggleForm.title" />
      </el-form-item>
        <el-form-item label="应用" prop="application_identity">
          <el-select v-model="joggleForm.application_identity" class="filter-item" placeholder="选择栏目">
            <el-option v-for="item in applicationOptions" :key="item.identity" :label="item.title" :value="item.identity" />
          </el-select>
        </el-form-item>
        <el-form-item label="模块" prop="modular_identity">
          <el-select v-model="joggleForm.modular_identity" class="filter-item" placeholder="选择来源">
            <el-option v-for="item in modularOptions" :key="item.identity" :label="item.title" :value="item.identity" />
          </el-select>
        </el-form-item>
        <el-form-item label="权限" prop="permission">
          <el-select v-model="joggleForm.permission" class="filter-item" placeholder="选择来源">
            <el-option v-for="item in permissionOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      <el-form-item label="发行人" prop="author">
        <el-input v-model="joggleForm.author" />
      </el-form-item>
      <el-form-item label="参数">
        <el-input v-model="joggleForm.parameter" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="参数" />
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="joggleForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="joggleForm.status.value === 5" type="danger" @click="handleDelete(joggleForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveJoggle, removeJoggleById, getJoggle } from '@/api/program/joggle'
import { getApplicationByUsable } from '@/api/program/application'
import { getModularsByUsable } from '@/api/program/modular'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'
import Upload from '@/components/Upload/Avatar' // secondary package based on el-pagination

export default {
  name: 'ComplexTable',
  components: { Upload },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      applicationOptions:[],
      modularOptions:[],
      permissionOptions:[
        {
          label:"public",
          value:1
        },
        {
          label:"user",
          value:2
        },
        {
          label:"client",
          value:3
        },
        {
          label:"admin",
          value:4
        },
        {
          label:"system",
          value:5
        }
      ],
      joggleForm: {
        title: '',
        avatar: '',
        author: '',
        status: {
          label: '',
          value: 1
        },
        remark: '',
        content: ''
      },
      joggleRule: []
    }
  },
  watch: {
    'joggleForm.application_identity':function(newVal,oldVal){
      this.loadModularList()
    }
  },
  created() {
    this.loadApplicationList()
    let joggleId = this.$route.query && this.$route.query.joggleId
    if (joggleId !== undefined) {
      joggleId = parseInt(joggleId)
      if (!isNaN(joggleId)) {
        this.listLoading = true
        const formData = {
          'joggleId': joggleId
        }
        getJoggle(formData).then(response => {
          if (response.status === 200) {
            this.joggleForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(joggleId)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'joggle' })
    },
    loadApplicationList() {
      getApplicationByUsable().then(response => {
        if(response.status === 200){
          this.applicationOptions = response.data.list
        }
      })
    },
    loadModularList() {
      const FormData = {
        "application_identity":this.joggleForm.application_identity
      }
      getModularsByUsable(FormData).then(response => {
        if(response.status === 200){
          this.modularOptions = response.data.list
        }
      })
    },
    handleResetForm() {
      this.joggleForm = {
        title: '',
        avatar: '',
        status: {
          label: '',
          value: 1
        },
        remark: '',
        content: ''
      }
    },
    createData() {
      const _joggleForm = this.joggleForm
      // return false;
      _joggleForm.status = 1
      if (_joggleForm.identity > 0) {
        _joggleForm.joggleId = _joggleForm.identity
      }
      console.log(this.joggleForm)
      saveJoggle(_joggleForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'joggle' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row) {
      const _this = this
      const formData = {
        'joggleId': row.identity
      }
      console.log(row)
      removeJoggleById(formData).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          _this.$router.push({ path: 'joggle' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
